﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;

namespace CS3230HealthTrackerSystemGUI
{
    public partial class RegisterPatient : Form
    {
        //Creates the variables to hold the information for the current patient
        //to be registered. 
        private String currentPatientFirstName;
        private String currentPatientLastName;
        private String currentPatientBirthDate;
        private String currentPatientAddressStreetNumber;
        private String currentPatientStreet;
        private String currentPatientAddressCity;
        private String currentPatientAddressState;
        private String currentPatientAddressZip;
        private String currentPatientContact;
        private Validation validate = new Validation();
        private Decryption key = new Decryption();

        public RegisterPatient()
        {
            InitializeComponent();
          
        }

        /// <summary>
        /// Registers the patient into the database. 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void registerPatientButton_Click(object sender, EventArgs e)
        {
            try
            {
                //Gets all the information for the patient from the text.
                currentPatientFirstName = patientFirstNameText.Text.Trim();
                currentPatientLastName = patientLastNameText.Text.Trim();
                currentPatientBirthDate = patientBirthdateText.Text;
                currentPatientAddressStreetNumber = patientAddressStreetNumberText.Text;
                currentPatientStreet = patientAddressStreetText.Text;
                currentPatientAddressCity = patientAddressCityText.Text;
                currentPatientAddressState = patientAddressStateText.Text;
                currentPatientAddressZip = patientAddressZipText.Text;
                currentPatientContact = patientContactNumberText.Text;

                //Gathers data from each of the text boxes and verify the data is valid. 
                validate.validateName(currentPatientFirstName, "First");
                validate.validateName(currentPatientLastName, "Last");
                validate.validatePhoneNumber(currentPatientContact);
                validate.validateDate(currentPatientBirthDate);
                validate.validateAddressStreetNumber(currentPatientAddressStreetNumber);
                validate.validateStateIntital(currentPatientAddressState);
                validate.validateStreetZipCode(currentPatientAddressZip);

                //Assigns Connection string.
                string MyConString = MyConString = key.returnConnectionString("xxjrmy", "xxjrmy", "xx3kz4z");
                OdbcConnection dataAccess = new OdbcConnection(MyConString);
                //Creates statement to insert all the information into the database.
                string myInsertQuery = "INSERT INTO PATIENT (firstName, lastName, dateOfBirth, addressStreetNumber, addressStreet, addressCity, addressState, addressZip, contactNumber) Values( '" + 
                                         currentPatientFirstName + "', '" + currentPatientLastName + "', '" + currentPatientBirthDate + "', '" + currentPatientAddressStreetNumber + "', '" 
                                         + currentPatientStreet + "', '" + currentPatientAddressCity + "', '" + currentPatientAddressState + "', '"
                                         + currentPatientAddressZip + "', '" + currentPatientContact + "')";
                
                //Sets up the connection, and Inserts the data.
                OdbcCommand insertCommand = new OdbcCommand(myInsertQuery);
                insertCommand.Connection = dataAccess;
                dataAccess.Open();
                insertCommand.ExecuteNonQuery();

                //Closes the insert command.
                insertCommand.Connection.Close();

                MessageBox.Show("Patient added to the database.");
                this.Close();
            }

           //Throws an exception if something is incorrectly formatted.
            catch (Exception formatException)
            {
                MessageBox.Show(formatException.Message);
            }
          
        }

        
    }
}
